package com.udhtu.ii.lab3;

public class App {

    public static double f(double x, double y)
    {
        return  Math.pow(x,4)+Math.pow(y,4)+2*x*x*y*y-4*x+3;
    }

    public static double g(double x, double y)
    {
        return  Math.pow(x-2,2)/3-3-y;
    }

    public static double C(double x, double y, double H)
    {
        return  f(x,y)+H*g(x,y);
    }

    public static double gradX(double x, double y)
    {
        return 4*Math.pow(x,3)+4*x*y*y-4;
    }

    public static double gradY(double x, double y)
    {
        return  4*Math.pow(y,3)+4*x*x*y;
    }

    public static double norm(double x, double y)
    {
        return Math.sqrt(gradX(x,y)*gradX(x,y)+gradY(x,y)*gradY(x,y));
    }

    public static double qwe(double x, double y)
    {
        if(g(x,y)>0)
            return 40000;
        else
            return 0;
    }


    public static void main(String args[])
    {
        double x=0.2, y=0.2;
        double x1=0.1, y1=0.1;
        double h=0.001;
        double H1=0;
        double H;
        H=qwe(x,y);
        while ( (C(x1,y1,H)-C(x,y,H)) > 0.00001)
        {
            x1=x;y1=y; H1=H;
            double gradx=gradX(x,y);
            double grady=gradY(x,y);
            double Norm=norm(x,y);
            x=x-h*gradx/Norm;
            y=y-h*grady/Norm;

            H=qwe(x,y);

        }
        System.out.println("gradient:\n"+x+" "+y+" "+C(x,y,H));

        x=0.2; y=0.2;
        x1=0.1; y1=0.1;
        h=0.00001;
        H1=0;
        H=qwe(x,y);
        while ( (C(x1,y1,H1)-C(x,y,H)) > 0.0000000001)
        {
            H1=H;
            if(Math.abs(gradX(x,y))<Math.abs(gradX(x+h,y)))
                x=x-h;
            while(  f(x-h,y)<f(x,y) )
            {    x=x-h;
            }
            if(Math.abs(gradX(x,y))>Math.abs(gradX(x+h,y)))
                x=x+h;
            while(  f(x-h,y)> f(x,y) )
            {    x=x+h;
            }


            if(Math.abs(gradY(x,y))<Math.abs(gradY(x,y+h)))
                y=y-h;
            while(  f(x,y-h)< f(x,y) )
            {    y=y-h;
            }
            if(Math.abs(gradY(x,y))>Math.abs(gradY(x,y+h)))
                y=y+h;
            while( f(x,y-h)> f(x,y) )
            {    y=y+h;
            }

            H=qwe(x,y);
            x1=x; y1=y;
        }
        System.out.println("spysk:"+x+" "+y+" "+C(x,y,H));
    }

}
